<template>
  <div class="page">
    <h1 style="color: #7f7f7f">请假记录</h1>
    <a-table
      stripe
      :columns="columns"
      :data="dataList"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
      @page-change="onPageChange"
    >
      <template #userAvatar="{ record }">
        <a-image width="64" :src="record.userAvatar" />
      </template>
      <template #resTime="{ record }">
        {{ dayjs(record.resTime).format("YYYY-MM-DD HH:mm:ss") }}
      </template>
      <template #leaveStart="{ record }">
        {{ enumapp[record.leaveStart] }}
      </template>
    </a-table>
  </div>
</template>

<script setup>
import { ref, watchEffect } from "vue";
import message from "@arco-design/web-vue/es/message";
import { dayjs } from "@arco-design/web-vue/es/_utils/date";
import { listMyLeaveTabVoByPageUsingPost } from "@/api/leaveTabController";

const enumapp = {
  1: "通过",
  0: "拒绝",
  2: "待审核",
};

// 初始化搜索条件（不应该被修改）
const initSearchParams = {
  current: 1,
  pageSize: 10,
};
const searchParams = ref({
  ...initSearchParams,
});
const dataList = ref([]);
const total = ref(0);

/**
 * 加载数据
 */
const loadData = async () => {
  const res = await listMyLeaveTabVoByPageUsingPost(searchParams.value);
  if (res.data.code === 0) {
    dataList.value = res.data.data?.records || [];
    total.value = res.data.data?.total || 0;
  } else {
    message.error("获取数据失败，" + res.data.message);
  }
};
/**
 * 当分页变化时，改变搜索条件，触发数据加载
 * @param page
 */
const onPageChange = (page) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

/**
 * 监听 searchParams 变量，改变时触发数据的重新加载
 */
watchEffect(() => {
  loadData();
});

// 表格列配置
const columns = [
  {
    title: "id",
    dataIndex: "id",
  },
  {
    title: "姓名",
    dataIndex: "leaveName",
  },
  {
    title: "类型",
    dataIndex: "type",
  },
  {
    title: "申请时间  ",
    dataIndex: "createTime",
  },
  {
    title: "审核状态",
    dataIndex: "leaveStart",
    slotName: "leaveStart",
  },
];
</script>
<style scoped>
.page {
  width: 100%;
  height: 100%;
  padding: 20px;
}
</style>
